package com.qdevelop.control.action;

import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class CustomManageAction extends QDevelopBaseAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1104640372121410108L;
	
	String key;
	int maxAge;
	Object value;

	public String setCookie()throws Exception{
		Cookie co = new Cookie(key,String.valueOf(value));
		co.setMaxAge(getMaxAge());
		co.setPath("/");
		ServletActionContext.getResponse().addCookie(co);
		return SUCCESS;
	}
	
	public String getCookie()throws Exception{
		Cookie cookies[] = ServletActionContext.getRequest().getCookies();
		for(Cookie co : cookies){
			if(co.getName().equals(key)){
				value = co.getValue();
				break;
			}
		}
		return SUCCESS;
	}
	
	public String setSession()throws Exception{
		ActionContext.getContext().getSession().put(key, value);
		return SUCCESS;
	}
	
	public String getSession()throws Exception{
		value = ActionContext.getContext().getSession().get(key);
		return SUCCESS;
	}
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}


	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public int getMaxAge() {
		return maxAge>0?maxAge:(60*60*24*365);
	}

	public void setMaxAge(int maxAge) {
		this.maxAge = maxAge;
	}
	
}
